{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59c56c44",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "585c6e33",
   "metadata": {},
   "outputs": [],
   "source": [
    "org_id = \"\"\n",
    "tembo_prod = \"https://api.tembo.io/api/v1\"\n",
    "token = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05cfff33",
   "metadata": {},
   "source": [
    "# Get all Instances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93b8e1ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.get(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"}\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "568960fe",
   "metadata": {},
   "source": [
    "# Create Instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60484d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.post(\n",
    "    f\"{tembo_prod}/orgs/{org_id}/instances\",\n",
    "     headers={\"Authorization\": f\"Bearer {token}\"},\n",
    "    json= {\n",
    "        'stack_type': 'Standard',\n",
    "        \"instance_name\": \"my-instance-12s\",\n",
    "        \"cpu\": '1',\n",
    "        \"memory\": '2Gi',\n",
    "        \"storage\": '10Gi',\n",
    "        \"replicas\": 1,\n",
    "        \"environment\": \"dev\"\n",
    "    }\n",
    "    \n",
    ")\n",
    "print(resp.status_code)\n",
    "instance = resp.json()\n",
    "instance"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2414910",
   "metadata": {},
   "source": [
    "# Get Instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ff393ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "inst_id = instance[\"instance_id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3daca89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.get(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances/{inst_id}\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"}\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0de8c52",
   "metadata": {},
   "source": [
    "# Patch Instance\n",
    "Update one or many attributes on an instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c48810",
   "metadata": {},
   "outputs": [],
   "source": [
    "inst_id = 'inst_1694536960658_bmrROG_3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "252a2df0",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.patch(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances/{inst_id}\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"},\n",
    "    json={\n",
    "        \"environment\": \"prod\",\n",
    "        \"extensions\": [],\n",
    "        \"trunk_installs\": [\n",
    "            {\n",
    "              \"name\": \"pgvector\",\n",
    "              \"version\": \"0.5.0\"\n",
    "            }\n",
    "        ],\n",
    "        \"extensions\": [\n",
    "            {\n",
    "                'name': 'vector',\n",
    "                'locations': [\n",
    "                    {\n",
    "                      \"database\": \"postgres\",\n",
    "                      \"enabled\": True,\n",
    "                      \"schema\": \"public\",\n",
    "                      \"version\": \"0.5.0\"\n",
    "                    }\n",
    "                ]\n",
    "            }\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af714c2",
   "metadata": {},
   "source": [
    "# PUT Instance\n",
    "Overwrite everything"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edcf291d",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.put(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances/{inst_id}\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"},\n",
    "    json={\n",
    "        'cpu': '2',\n",
    "        'memory': '2Gi',\n",
    "        'storage': '10Gi',\n",
    "        'environment': 'prod',\n",
    "        'replicas': 1\n",
    "    }\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff610d1c",
   "metadata": {},
   "source": [
    "# POST Instance (lifecylce events)\n",
    "Restart instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c48d7ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.post(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances/{inst_id}\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"},\n",
    "    params={\"event_type\": \"restart\"}\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5953bbac",
   "metadata": {},
   "source": [
    "# Delete Instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cec4d051",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = requests.delete(\n",
    "    url=f\"{tembo_prod}/orgs/{org_id}/instances/{inst_id}\",\n",
    "    headers={\"Authorization\": f\"Bearer {token}\"},\n",
    ")\n",
    "print(resp.status_code)\n",
    "resp.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ec6c7a4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
